﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.ScrumTool.Service.Interface;
using UIT.ScrumTool.DTO;
using UIT.ScrumTool.Service.Model;
using System.Globalization;

namespace UIT.ScrumTool.Service.Business
{
    public partial class ControlService : IProjectService
    {
        // Get All Project In DB
        public List<ProjectDto> ProjectGetByAll()
        {
            List<ProjectDto> lsprojectDto = new List<ProjectDto>();
            try
            {
                List<Project> ls = dataContext.sp_Project_GetByAll().ToList();
                foreach (var project in ls)
                {
                    lsprojectDto.Add(ControlService.ProjectConvertProjectDto(project));
                }
                return lsprojectDto;
            }
            catch (Exception)
            {
                return null;
            }
            
        }

        // Get Project In DB with ID
        public ProjectDto ProjectGetById(int id)
        {
            try
            {
                return ControlService.ProjectConvertProjectDto(dataContext.sp_Project_GetById(id).Single());
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Get List Project In DB with condition (top, where, order)
        public List<ProjectDto> ProjectGetByTop(string top, string where, string order)
        {
            List<ProjectDto> lsprojectDto = new List<ProjectDto>();
            try
            {
                string query = "Select * from [Employee]";
                if (!top.Equals(""))
                    query = " Select top " + top + " * from [Project] ";
                if (!where.Equals(""))
                    query += " where " + where + " ";
                if (!order.Equals(""))
                    query += " order by " + order;

                IEnumerable<Project> results = dataContext.ExecuteQuery<Project>(@query);
                List<Project> ls = results.ToList();
                foreach (var project in ls)
                {
                    lsprojectDto.Add(ControlService.ProjectConvertProjectDto(project));
                }
                return lsprojectDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Insert Project In DB
        public int ProjectInsert(ProjectDto projectDto)
        {
            Project project = new Project();
            project.Name = projectDto.Name;
            project.Start = DateTime.ParseExact(projectDto.Start, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            project.Description = projectDto.Description;
            project.Logo = projectDto.Logo;
            project.PrivateProject = projectDto.PrivateProject;
            project.HomePage = projectDto.HomePage;
            project.Status = projectDto.Status;

            try
            {
                dataContext.Projects.InsertOnSubmit(project);
                dataContext.SubmitChanges();
                return project.Id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        // Update Project in DB
        public bool ProjectUpdate(ProjectDto project)
        {
            try
            {
                dataContext.sp_Project_Update(project.Id, project.Name, DateTime.ParseExact(project.Start, "dd/MM/yyyy", CultureInfo.InvariantCulture), 
                    project.Description, project.Logo, project.PrivateProject, project.HomePage, project.Status);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Delete Project in DB
        public bool ProjectDelete(int id)
        {
            try
            {
                dataContext.sp_Project_Delete(id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Number of Project
        public int ProjectCount()
        {
            return dataContext.Projects.Count();
        }

        // Get Id of Last Project
        public int LastProjectId()
        {
            return 0;
        }

        // Convert Project to ProjectDto
        public static ProjectDto ProjectConvertProjectDto(Project project)
        {
            ProjectDto projectDto = new ProjectDto();
            projectDto.Id = project.Id;
            projectDto.Logo = project.Logo;
            projectDto.Name = project.Name;
            if (project.Start == null)
            {
                project.Start = null;
            }
            else
            {
                projectDto.Start = ((DateTime)project.Start).ToString("dd/MM/yyyy");
            }
            // status: Finished: 1 or not Finish: 0
            projectDto.Status = (int)project.Status;
            projectDto.HomePage = project.HomePage;
            projectDto.Description = project.Description;
            //private project: 1 or public project: 0
            projectDto.PrivateProject = (int)project.PrivateProject;
            return projectDto;
        }
    }
}
